package com.jinsi.controller;

import com.jinsi.pojo.User;
import com.jinsi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
/*
密码加密
MD5 加密

 */
@Controller
public class LoginController {

    @Autowired
    private UserService userService;


    @ResponseBody
    @RequestMapping("/login")
    public Map<String,String> login(String username,String password){
        Map<String,String> map = new HashMap<>();
        User user = userService.getUserByNameAndPwd(username, null);
        if(user != null){

            // 验证密码
            String oldpwd = user.getPassword(); // 数据库的密码
            // 使用username作为盐值  -- 再次加密生成密码
            String newpwd = DigestUtils.md5DigestAsHex((username + password).getBytes());
            // 比较数据库密码与再次生成的密码，一致则密码正确，否则不正确
            if(oldpwd.equals(newpwd)){
                // 登录成功
                map.put("state","200");
                map.put("msg","登录成功");
            }else{
                // 密码错误
                map.put("state","301");
                map.put("msg","密码错误");
            }

        }else{
            // 登录失败，用户名不存在
            map.put("state","300");
            map.put("msg","用户名不存在");

        }

        return map;
    }

}
